<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 设置主题
 *
 * @author      Eden
 * @datetime    2016/12/10 22:46
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Slider extends ADM_Controller
{

    const PAGE_SIZE = 20;

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主页面
     */
    public function index()
    {
        $this->load->view(parent::TMP_DIR . '/bbs/slider/index.html');
    }

    /**
     * 添加幻灯
     */
    public function add()
    {
        $this->load->view(parent::TMP_DIR . '/bbs/slider/add.html');
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $this->load->view(parent::TMP_DIR . '/bbs/slider/edit.html');
    }

    /**
     * 列表
     */
    public function lists()
    {
        $send['state'] = false;
        do {

            $page = $this->input->get('page');
            $page = !empty($page) && intval($page) > 1 ? intval($page) : 1;
            $offset = ($page - 1) * self::PAGE_SIZE;

            $this->load->model('admin/bbs/Slider_Model', 'slider');
            if ($results = $this->slider->getLists($offset, self::PAGE_SIZE)) {
                if (!empty($results) && is_array($results['lists'])) foreach ($results['lists'] as &$list) {
                    $list['thumb'] = thumb($list['thumb'], 158, 60);
                }
                $send['state'] = true;
                $send['data'] = [
                    'nextPage' => false,
                    'data' => $results['lists']
                ];
            } else {
                $send['error'] = '添加失败';
            }

        } while (false);
        $this->output->send($send);
    }

    /**
     * 提交
     */
    public function submit()
    {
        $send['state'] = false;
        do {

            $post = $this->input->post();
            if (empty($post['title']) || empty($post['source']) || empty($post['timestart']) || empty($post['timeend'])
                || empty($post['thumb']) || empty($post['type']) || empty($post['value'])
            ) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/bbs/Slider_Model', 'slider');
            if ($this->slider->submit($post)) {
                $send['state'] = true;
            } else {
                $send['error'] = '添加失败';
            }

        } while (false);
        $this->output->send($send);
    }

    /**
     * 提交
     */
    public function editSlider()
    {
        $send['state'] = false;
        do {

            $post = $this->input->post();
            if (empty($post['id']) || empty($post['title']) || empty($post['source']) || empty($post['timestart']) || empty($post['timeend'])
                || empty($post['thumb']) || empty($post['type']) || empty($post['value'])
            ) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/bbs/Slider_Model', 'slider');
            if ($this->slider->edit($post)) {
                $send['state'] = true;
            } else {
                $send['error'] = '修改失败';
            }

        } while (false);
        $this->output->send($send);
    }

    /**
     * 详情
     */
    public function detail()
    {
        $send['state'] = false;
        do {

            $sliderId = $this->input->get('sliderId');
            if (empty($sliderId)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/bbs/Slider_Model', 'slider');
            if ($detail = $this->slider->detail($sliderId)) {
                $detail['viewThumb'] = thumb($detail['thumb'], 158, 60);

                if ($detail['type'] == '2') {
                    $this->load->model('admin/base/Common_Model', 'base');
                    $detail['value'] = $this->base->getDetail($detail['value']);
                }

                if ($detail['type'] == '3') {
                    $this->load->model('admin/goods/Goods_Model', 'goods');
                    $detail['value'] = $this->goods->getDetail($detail['value']);
                }

                $send['state'] = true;
                $send['data'] = $detail;
            } else {
                $send['error'] = '添加失败';
            }

        } while (false);
        $this->output->send($send);
    }

    public function deleteSlider()
    {
        $send['state'] = false;
        do {

            $id = $this->input->get('sliderId');
            if (empty($id)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/bbs/Slider_Model', 'slider');
            if ($this->slider->deleteItem($id)) {
                $send['state'] = true;
            } else {
                $send['error'] = '修改失败';
            }

        } while (false);
        $this->output->send($send);
    }

    /**
     * 修改状态
     */
    public function changeStatus()
    {
        $send['state'] = false;
        do {

            $id = $this->input->get('sliderId');
            if (empty($id)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/bbs/Slider_Model', 'slider');
            if ($this->slider->changeStatus($id)) {
                $send['state'] = true;
            } else {
                $send['error'] = '修改失败';
            }

        } while (false);
        $this->output->send($send);
    }

    /**
     * 获取配置
     */
    public function getSetting()
    {
        $send['state'] = false;
        $this->load->model('common/Setting_Model', 'sets');
        $res = $this->sets->getSetting('system', 'bbs');
        if ($res) {
            $res['viewThumb'] = thumb($res['thumb'], 100, 100);
            $send['state'] = true;
            $send['data'] = $res;
        } else {
            $send['data'] = [];
        }
        $this->output->send($send);
    }

    /**
     * 设置配置
     */
    public function setSetting()
    {
        $send['state'] = false;
        do {

            $params = $this->input->post();
            if (empty($params['nickname']) || empty($params['thumb'])) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('common/Setting_Model', 'sets');
            if ($this->sets->setSetting('system', 'bbs', $params)) {
                $send['state'] = true;
            } else {
                $send['error'] = '设置失败';
            }
        }while(false);
        $this->output->send($send);
    }
}
